package yang.yu.sunny.utils;

import org.dayatang.domain.EntityRepository;
import yang.yu.sunny.domain.*;

import java.util.Date;
import java.util.Set;

/**
 * Created by yyang on 16/3/23.
 */
public class EntityHelper {

    public ProductCategory createProductCategory(String name) {
        ProductCategory result = new ProductCategory(name);
        result.save();
        return result;
    }

    public Product createProduct(String name, ProductCategory category) {
        Product result = new Product(name, category);
        result.save();
        return result;
    }

    public ProductPrice createProductPrice(Product product, double price) {
        ProductPrice result = new ProductPrice(product, price);
        result.save();
        return result;
    }

    public ProductPrice createProductPrice(Product product, double unitPrice, Date time) {
        ProductPrice result = new ProductPrice(product, unitPrice, time);
        result.save();
        return result;
    }

    public Customer createCustomer(String name) {
        Customer result = new Customer(name);
        result.save();
        return result;
    }

    public Order createOrder(Customer customer, Set<OrderItem> items) {
        return createOrder(customer, items, new Date());
    }

    public Order createOrder(Customer customer, Set<OrderItem> items, Date date) {
        Order result = new Order();
        result.setCustomer(customer);
        result.setCreated(date);
        result.setItems(items);
        result.save();
        return result;
    }

    public Order2 createOrder2(Customer customer, Set<OrderItem2> items) {
        return createOrder2(customer, items, new Date());
    }

    private Order2 createOrder2(Customer customer, Set<OrderItem2> items, Date date) {
        Order2 result = new Order2();
        result.setCustomer(customer);
        result.setCreated(date);
        result.setItems(items);
        result.save();
        return result;
    }
}
